/**
 * 1：进入游戏后选择任务后，点击加速按钮为艾迪加速，
 *    3秒内可能加速到速度：1、2、3、4、8、10、13，
 *    结果的计算方法为：60÷（2+X）的结果（x为前面的加速度数字），
 *    根据正确与否反馈
 * 2：进入游戏后选择任务后，点击加速按钮为艾迪加速，
 *    3秒内可能加速到速度：14、15、16、17，
 *    结果的计算公式是530-210÷（X-10）×10 的结果（x为前面的加速度数字），
 *    根据正确与否反馈
 */
// parent
var $app;
// 资源
var resources;
// 按钮
var $startBtn,$select_startBtn,$taskBBtn,$speedBtn,$confirmBtn,$rescueBtn,$resultBtn,$replayBtn,$returnBtn;
// 输入框
// var $resultInput;
// layer
var $start_layer,$select_start_layer,$huo_layer,
	$base_layer,$train_layer,$thief_layer,$chara_layer,$tips_layer,$game_layer,$confirm_layer,$qian_layer;
// CanvasRenderingContext2D
var start_2d,select_start_2d,huo_2d,
	base_2d,train_2d,chara_2d,tips_2d,game_2d,confirm_2d,qian_2d;
var $keyboard,$keyboardKey;
var $success,$error;
var $progress;
window.onload=function(){
	$html           = G.dom('html');
	$app            = G.dom('#app');
	$startBtn       = G.dom('#startBtn');
	$taskBBtn       = G.dom('#taskBBtn');
	$speedBtn       = G.dom('#speedBtn');
	$select_startBtn= G.dom('#select_startBtn');
	$confirmBtn     = G.dom('#confirmBtn');
	$rescueBtn      = G.dom('#rescueBtn');
	$resultBtn      = G.dom('#resultBtn');
	$replayBtn      = G.dom('#replayBtn');
	$returnBtn      = G.dom('#returnBtn');
	$keyboard       = G.dom('#keyboard');
	$keyboardKey    = G.dom('#keyboardKey');
	$success        = G.dom('#success');
	$error          = G.dom('#error');
	$progress       = G.dom('#progress');

	// canvas
	$start_layer       = G.dom('#start_layer');
	$huo_layer         = G.dom('#huo_layer');
	$select_start_layer= G.dom('#select_start_layer');
	$base_layer        = G.dom('#base_layer');
	$train_layer        = G.dom('#train_layer');
	$chara_layer       = G.dom('#chara_layer');
	$tips_layer        = G.dom('#tips_layer');
	$game_layer        = G.dom('#game_layer');
	$confirm_layer     = G.dom('#confirm_layer');
	$qian_layer        = G.dom('#qian_layer');
	start_2d           = $start_layer.getContext('2d');
	huo_2d             = $huo_layer.getContext('2d');
	select_start_2d    = $select_start_layer.getContext('2d');
	base_2d            = $base_layer.getContext('2d');
	train_2d           = $train_layer.getContext('2d');
	chara_2d           = $chara_layer.getContext('2d');
	tips_2d            = $tips_layer.getContext('2d');
	game_2d            = $game_layer.getContext('2d');
	confirm_2d         = $confirm_layer.getContext('2d');
	qian_2d            = $qian_layer.getContext('2d');

	// 屏幕适应
	G.resize($app,4,3);
	// 加载图片
	var load = new G.load();
	load.media([
		{name : 'background', path : './images/background.png', type : 'img'},
		{name : 'boy', path : './images/boy.png', type : 'img'},
		{name : 'title_anim', path : './images/title_anim.png', type : 'img'},
		{name : 'huo', path : './images/huo.png', type : 'img'},
		{name : 'huo_anim', path : './images/huo_anim.png', type : 'img'},
		{name : 'background2', path : './images/background2.png', type : 'img'},
		{name : 'taskB_title', path : './images/taskB_title.png', type : 'img'},
		{name : 'water', path : './images/water.png', type : 'img'},
		{name : 'train_anim', path : './images/train_anim.png', type : 'img'},
		{name : 'tips_anim', path : './images/tips_anim.png', type : 'img'},
		{name : 'chara_anim', path : './images/chara_anim.png', type : 'img'},
		{name : 'game_speed', path : './images/game_speed.png', type : 'img'},
		{name : 'confirm', path : './images/confirm.png', type : 'img'},
		{name : 'game_dataload', path : './images/game_dataload.png', type : 'img'},
		{name : 'game_info', path : './images/game_info.png', type : 'img'},
		{name : 'sub', path : './images/sub.png', type : 'img'},
		{name : 'game_chara_error_anim', path : './images/game_chara_error_anim.png', type : 'img'},
		{name : 'game_chara_success_anim', path : './images/game_chara_success_anim.png', type : 'img'},
		{name : 'game_train_error_anim', path : './images/game_train_error_anim.png', type : 'img'},
		{name : 'game_train_success_anim', path : './images/game_train_success_anim.png', type : 'img'},
		
		{name : 'start_music', path : './music/start_music.mp3', type : 'audio'},
		{name : 'train_music', path : './music/train_music.mp3', type : 'audio'},
		{name : 'fall_music', path : './music/fall_music.mp3', type : 'audio'},
		{name : 'dizzy_music', path : './music/dizzy_music.mp3', type : 'audio'},
		{name : 'win_music', path : './music/win_music.mp3', type : 'audio'},
		{name : 'win2_music', path : './music/win2_music.mp3', type : 'audio'},
		{name : 'click_music', path : './music/click_music.mp3', type : 'audio'}
		// {name : 'A', path : './audio/A.mp3', type : 'audio'}
	],function(ret){
		console.log('图片加载进度：',ret);
		$progress.style.width=(ret*100)+'%';
	},gameInit);
};
// 屏幕适应
window.onresize=function(){
	G.resize($app,4,3);
};
// 进入游戏初始化
function gameInit(ret){
	G.hide($progress);
	// 显示启动游戏层
	G.show($start_layer);
	// 缓存图片
	resources = ret;

	/**
	 * 进行启动游戏层的制作
	 */
	renderStartLayer.call(start_2d);
	/**
	 * 进行选择任务层的制作
	 */
	renderSelectStartLayer.call(select_start_2d);
	/**
	 * 进行游戏层的制作
	 */
	
	renderQianLayer.call(qian_2d);

	// 事件初始化
	$startBtn.addEventListener('click',startTo,false);
	$taskBBtn.addEventListener('click',selectTaskB,false);
	$select_startBtn.addEventListener('click',selectStartTo,false);
	$confirmBtn.addEventListener('click',confirmTo,false);
	$speedBtn.addEventListener('click',addSpeedTo,false);
	$rescueBtn.addEventListener('click',rescueTo,false);
	$resultBtn.addEventListener('click',resultTo,false);
	$replayBtn.addEventListener('click',replayTo,false);
	$returnBtn.addEventListener('click',returnTo,false);
	$keyboardKey.addEventListener('click',keyboardKeyTo,false);

	resources['train_music'].loop='loop';
}
// 开始按钮点击
function startTo(){
	resources['click_music'].play();
	// console.log('开始');
	G.hide($start_layer);
	G.hide($huo_layer);
	G.hide($startBtn);
	// 显示选择任务层
	G.show($select_start_layer);
	// 显示任务A
	G.show($taskBBtn);
	// 显示选择开始按钮
	G.show($select_startBtn);
}
// taskB被选中
var taskBAble=false;
function selectTaskB(){
	taskBAble=!taskBAble;
	if(taskBAble){
		resources['click_music'].play();
		$taskBBtn.className='btn taskB active';
	}else{
		$taskBBtn.className='btn taskB';
	}
}
// 选择开始按钮点击
function selectStartTo(){
	if(taskBAble){
		resources['click_music'].play();
		// console.log('选择开始');
		// 隐藏任务层
		G.hide($select_start_layer);
		G.hide($taskBBtn);
		G.hide($select_startBtn);
		// 显示游戏层
		G.show($qian_layer);
		G.show($base_layer); // 1E2A3E
		G.show($train_layer);
		G.show($chara_layer);
		G.show($tips_layer);
		G.show($game_layer);
		G.show($confirm_layer);

		renderBaseLayer.call(base_2d);
		renderTrainLayer.call(train_2d);
		renderTipsLayer.call(tips_2d);
		setTimeout(function(){
			resources['train_music'].play();
		},300);
	}
}
// 加速点击
var speedLimit=[14,15,16,17];
function addSpeedTo(){
	resources['click_music'].play();
	if(!speedComplete){
		speeda+=0.2;
		var s=Math.floor(speeda);
		for(var i=0;i<7;i++){
			if(s==speedLimit[i]){
				speed=s;
			}
		}
		// 显示速度
		game_2d.clearRect(414,0,610,320);
		game_2d.drawImage(resources['game_speed'],414,0,610,320);
		// game_2d.drawImage(resources['sub'],650,190,67,42);
		game_2d.save();
		game_2d.fillStyle = '#ffffff';
		game_2d.font='bolder 35px Microsoft YaHei';
		game_2d.fillText(speed+'',660,225);
		// game_2d.font='bolder 35px Microsoft YaHei';
		// game_2d.fillText('米 / 秒',730,220);
		game_2d.restore();
	}else{
		console.log('数据分析中');
	}
}
// 确认层的点击
var charaAnimSub;
function confirmTo(){
	resources['click_music'].play();
	// console.log('确认');
	G.hide($confirm_layer);
	G.hide($confirmBtn);
	// 显示加速按钮
	G.show($speedBtn);
	// 监听加速参数
	addListener();
	// 人物动画
	var charaAnimArr=[
		[0,0,70,75],[70,0,61,74],[131,0,56,71],[187,0,68,72],
		[0,75,70,75],[70,75,64,72],[134,75,56,70],
		[0,150,66,71]
	],charaIndex=0;
	charaAnimSub=setInterval(function(){
		var t=charaAnimArr[charaIndex];
		chara_2d.clearRect(0,475,t[2],t[3]);
		chara_2d.drawImage(resources['chara_anim'],t[0],t[1],t[2],t[3],0,475,t[2],t[3]);
		if(++charaIndex>=8){
			charaIndex=0;
		}
	},100);
}
// 实施营救按钮点击
var isWin=false;
function rescueTo(){
	resources['click_music'].play();
	G.hide($keyboard);
	G.hide($keyboardKey);
	G.hide($rescueBtn);
	G.hide($game_layer);
	console.log('实施营救');
	clearInterval(TrainAnimSub);
	clearInterval(charaAnimSub);
	// 530-210÷（X-10）×10
	if(530-210/(speed-10)*10==resultNum){
		console.log('营救成功');
		toSuccess();
	}else{
		console.log('营救失败');
		toError();
	}
}
// 结果的输入
function resultTo(){
	resources['click_music'].play();
	// console.log('结果');
	G.show($keyboard);
	G.show($keyboardKey);
}
// 键盘点击
var resultStr='0',resultNum=0;
function keyboardKeyTo(e){
	var el = e.target,flag=el.getAttribute('flag');
	if(!flag)return false;
	resources['click_music'].play();
	if(flag=='del'){//删除
		console.log('删除');
		if(resultStr.length==1){
			resultStr='0';
		}else if(resultStr.length>1){
			resultStr=resultStr.slice(0,-1);
		}
		resultNum=parseInt(resultStr,10);
		console.log(resultStr,resultNum);
	}else if(flag=='enter'){//确定
		console.log('确定');
		G.hide($keyboard);
		G.hide($keyboardKey);
	}else if(resultNum<100){
		resultStr+=flag;
		resultNum=parseInt(resultStr,10);
		console.log(resultStr,resultNum);
	}
	game_2d.clearRect(414,0,610,320);
	game_2d.drawImage(resources['game_info'],414,0,610,320);
	// 显示文字
	game_2d.save();
	game_2d.fillStyle = '#ffffff';
	game_2d.font='bolder 25px Microsoft YaHei';
	game_2d.fillText(''+speed,690,225);
	game_2d.font='bolder 30px Microsoft YaHei';
	game_2d.fillText(resultNum+'',845,265);
	game_2d.restore();
}
// 重玩点击
function replayTo(){
	resources['click_music'].play();
	resultStr='0';
	resultNum=0;
	G.hide($replayBtn);
	G.hide($returnBtn);
	G.hide($success);
	G.hide($error);
	// 隐藏任务层
	G.hide($select_start_layer);
	G.hide($taskBBtn);
	G.hide($select_startBtn);

	// 显示游戏层
	G.show($qian_layer);
	G.show($base_layer); // 1E2A3E
	G.show($train_layer);
	G.show($chara_layer);
	G.show($tips_layer);
	G.show($game_layer);
	G.show($confirm_layer);
	console.log('重玩');
	base_2d.clearRect(0,0,1024,768);
	train_2d.clearRect(0,0,1024,768);
	chara_2d.clearRect(0,0,1024,768);
	tips_2d.clearRect(0,0,1024,768);
	game_2d.clearRect(0,0,1024,768);
	confirm_2d.clearRect(0,0,1024,768);
	// 进入下一屏
	renderTrainLayer2.call(train_2d);
}
// 返回点击
function returnTo(){
	resources['click_music'].play();
	resultStr='0';
	resultNum=0;
	G.hide($replayBtn);
	G.hide($returnBtn);
	G.hide($success);
	G.hide($error);
	console.log('返回');
	// 显示任务层
	G.show($select_start_layer);
	G.show($taskBBtn);
	G.show($select_startBtn);

	// 隐藏游戏层
	G.hide($qian_layer);
	G.hide($base_layer); // 1E2A3E
	G.hide($train_layer);
	G.hide($chara_layer);
	G.hide($tips_layer);
	G.hide($game_layer);
	G.hide($confirm_layer);
	base_2d.clearRect(0,0,1024,768);
	train_2d.clearRect(0,0,1024,768);
	chara_2d.clearRect(0,0,1024,768);
	tips_2d.clearRect(0,0,1024,768);
	game_2d.clearRect(0,0,1024,768);
	confirm_2d.clearRect(0,0,1024,768);
}
/**
 * 进行启动游戏层的制作【静态】
 */
function renderStartLayer(){
	resources['start_music'].play();
	var s = this;
	var r=1,ar=20,globalAlpha=0,aglobalAlpha=0.02,startAnim=setInterval(function(){
		s.clearRect(0,0,1024,768);
		s.save();
		s.fillRect(0,0,1024,768);
		s.globalAlpha=(globalAlpha+=aglobalAlpha);
		var grd=s.createRadialGradient(512,384,50,512,384,r+50);
		grd.addColorStop(0,'#ffffff');
		grd.addColorStop(1,'#000000');
		s.fillStyle=grd;
		s.beginPath();
		s.arc(512,384,r+=ar,0,2*Math.PI);
		s.fill();
		s.restore();
		if(r>=1000){
			clearInterval(startAnim);
			s.clearRect(0,0,1024,768);
			s.drawImage(resources['background'],0,0,1024,768);
			s.drawImage(resources['huo'],232,238,560,400);
			renderHuoLayer.call(huo_2d);
			// 显示火+使命召唤动画
			G.show($huo_layer);
		}
	},1000/60);
}
/**
 * 火动画层的绘制
 */
function renderHuoLayer(){
	var s = this;
	// 火动画 + 使命召唤字幕动画
	var huoAnimArr=[
		[0,0,138,115],
		[0,115,138,115],
		[0,234,138,115],
		[0,353,138,115]
	],fontAnimArr=[
		[0,0,693,278],[693,0,665,278],[1358,0,693,278],
		[0,278,693,278],[693,278,665,278],[1358,278,693,278],
		[0,556,693,278],[693,556,665,278],[1358,556,693,278],
		[0,834,693,278],[693,834,665,278],[1358,834,693,278],
		[0,1112,693,278],[693,1112,665,278],[1358,1112,693,278],
		[0,1390,693,278],[693,1390,665,278],[1358,1390,693,278]
	],huoIndex=0,fontIndex=0,Anim=setInterval(function(){
		s.clearRect(0,0,1024,768);
		var h=huoAnimArr[huoIndex],f=fontAnimArr[fontIndex];
		s.drawImage(resources['huo_anim'],h[0],h[1],h[2],h[3],372,425,280,200);
		s.drawImage(resources['title_anim'],f[0],f[1],f[2],f[3],(1024-f[2])/2,425,f[2],f[3]);
		s.drawImage(resources['boy'],412,238,200,530);
		if(++huoIndex>=4)huoIndex=0;
		if(++fontIndex>=18){
			clearInterval(Anim);
			// 开始按钮显示
			G.show($startBtn);
		}
	},50);
}
/**
 * 进行选择任务层的制作【静态】
 */
function renderSelectStartLayer(){
	var s = this;

	s.drawImage(resources['background2'],0,0,1024,768);

	s.save();
	s.fillStyle = '#ffffff';
	s.font='bolder 35px Microsoft YaHei';
	s.fillText('请选择一个绝密任务 ',350,174);
	s.restore();
}
/**
 * 进行游戏层的制作
 */
function renderBaseLayer(){
	var s = this;

	s.clearRect(0,0,1024,768);
	s.save();
	s.fillStyle='#1E2A3E';
	s.fillRect(0,0,1024,768);
	s.drawImage(resources['water'],0,0,1024,400,0,368,1024,400);
	s.restore();
}
function renderBaseLayer2(){
	var s = this;
	
	s.clearRect(0,0,1024,768);
	s.save();
	s.fillStyle='#1E2A3E';
	s.fillRect(0,0,1024,768);
	s.drawImage(resources['water'],1000,0,1024,400,0,368,1024,400);
	s.restore();
}
function renderTrainLayer(){
	var s = this;
	// 火车动画
	var trainAnimArr=[
		[0,0,211,68],[211,0,211,68],
		[0,68,211,67],[211,68,211,67],
		[0,135,211,67],[211,135,211,67],
		[0,202,211,67],[211,202,211,67],
		[0,269,211,67],[211,269,211,67],
		[0,336,211,67],[211,336,211,67],
		[0,403,211,67],[211,403,211,67]
	],trainIndex=0,x=0,Anim=setInterval(function(){
		s.clearRect(0,0,1024,768);
		var t=trainAnimArr[trainIndex];
		s.drawImage(resources['train_anim'],t[0],t[1],t[2],t[3],x,450,t[2],t[3]);
		if(++trainIndex>=14){
			trainIndex=0;
			// clearInterval(Anim);
		}
		if((x+=5)>=1024){
			clearInterval(Anim);
			resources['train_music'].pause();
			// 进入下一屏
			renderTrainLayer2.call(s);
		}
		if(x==800){
			// 提示牌移动上去
			renderTipsLayer2.call(tips_2d);
		}
	},1000/60);
}
// 进入下一屏
var TrainAnimSub;
function renderTrainLayer2(){
	var s=this;
	renderBaseLayer2.call(base_2d);
	// 火车动画
	var trainAnimArr=[
		[0,0,211,68],[211,0,211,68],
		[0,68,211,67],[211,68,211,67],
		[0,135,211,67],[211,135,211,67],
		[0,202,211,67],[211,202,211,67],
		[0,269,211,67],[211,269,211,67],
		[0,336,211,67],[211,336,211,67],
		[0,403,211,67],[211,403,211,67]
	],trainIndex=0,x=-211,Anim=setInterval(function(){
		s.clearRect(0,0,1024,768);
		var t=trainAnimArr[trainIndex];
		s.drawImage(resources['train_anim'],t[0],t[1],t[2],t[3],x,450,t[2],t[3]);
		if(++trainIndex>=14){
			trainIndex=0;
		}
		if((x+=5)>=50){
			clearInterval(Anim);
			// 绘制人
			chara_2d.drawImage(resources['chara_anim'],0,150,66,71,0,475,66,71);
			// 加速提示牌动画
			renderGameLayer.call(game_2d);
			// 绘制火车子动画
			TrainAnimSub=setInterval(function(){
				s.clearRect(0,0,1024,768);
				var t=trainAnimArr[trainIndex];
				s.drawImage(resources['train_anim'],t[0],t[1],t[2],t[3],x,450,t[2],t[3]);
				if(++trainIndex>=14){
					trainIndex=0;
				}
			},1000/60);
		}
	},1000/60);

}
function renderGameLayer(){
	var s = this;
	// 加速提示牌动画
	var y=-320,Anim2=setInterval(function(){
		s.clearRect(414,y,610,320);
		s.drawImage(resources['game_speed'],414,y,610,320);
		if((y+=10)>0){
			clearInterval(Anim2);
			G.show($confirm_layer);
			renderConfirmLayer.call(confirm_2d);
		}
	},1000/60);
}
function renderTipsLayer(){
	var s = this;
	// 提示牌动画
	var tipsAnimArr=[
		[0,0,580,459],[580,0,580,452],[1160,0,580,452],
		[0,459,580,468],[580,459,580,468],[1160,459,580,468],
		[0,927,578,441],[578,927,585,441],[1163,927,578,441],
		[0,1368,577,419],[577,1368,580,419],[1157,1368,580,419],
		[0,1787,579,437],[579,1787,576,437],[1155,1787,578,437],
		[0,2224,579,450],[579,2224,580,450],[1159,2224,580,450],
		[0,2674,579,452],[579,2674,580,452],[1159,2674,580,452],
		[0,3126,579,452],[579,3126,579,452],[1159,3126,580,452]
	],tipsIndex=4,y=-350,Anim=setInterval(function(){
		s.clearRect(0,0,1024,768);
		var t=tipsAnimArr[tipsIndex];
		s.drawImage(resources['tips_anim'],t[0],t[1],t[2],t[3],(1024-t[2])/2,y,t[2],t[3]);
		if(y<-150){
			y+=10;
		}else{
			if(++tipsIndex>=24){
				clearInterval(Anim);
			}
		}
	},1000/60);
}
// 提示牌移动上去
function renderTipsLayer2(){
	var s = this;
	// 提示牌动画
	var tipsIndex=0,y=-150,Anim=setInterval(function(){
		s.clearRect(0,0,1024,768);
		s.drawImage(resources['tips_anim'],1159,3126,580,452,(1024-580)/2,y,580,452);
		if((y-=10)<-452){
			clearInterval(Anim);
		}
	},1000/60);
}
function renderConfirmLayer(){
	var s = this;
	G.show($confirmBtn);
	s.drawImage(resources['confirm'],0,0,1024,768);
}
function renderQianLayer(){
	var s = this;
	s.drawImage(resources['taskB_title'],20,20,330,90);
}
// 监听加速参数
var speed,speedComplete,speeda;
function addListener(){
	speed=14;
	speeda=14;
	speedComplete=false;
	// 1、2、3、4、8、10、13
	var listener=setTimeout(function(){
		clearTimeout(listener);
		G.hide($speedBtn);
		speedComplete=true;
		speeda=14;
		// 绘制数据在分析中...
		game_2d.clearRect(414,0,610,320);
		game_2d.drawImage(resources['game_dataload'],414,0,610,320);
		// 模拟数据分析动画
		setTimeout(function(){
			game_2d.clearRect(414,0,610,320);
			game_2d.drawImage(resources['game_info'],414,0,610,320);
			// 显示文字
			game_2d.save();
			game_2d.fillStyle = '#ffffff';
			game_2d.font='bolder 25px Microsoft YaHei';
			game_2d.fillText(''+speed,690,225);
			game_2d.restore();
			G.show($resultBtn);
		},1000);
		// 实施营救
		G.show($rescueBtn);
	},3000);
}
function toSuccess(){
	resources['train_music'].play();
	// 火车成功动画
	var trainSuccessAnimArr=[
		[0,0,209,64],
		[0,64,209,67],
		[0,131,209,66],
		[0,197,209,66],
		[0,263,209,65],
		[0,328,209,65],
		[0,393,209,65],
		[0,458,209,65],
		[0,523,209,66],
		[0,589,209,67],
		[0,656,209,67],
		[0,723,209,66],
		[0,789,209,66],
		[0,855,209,66],
		[0,921,209,66]
	],trainSuccessIndex=0,trainSuccessX=50,trainSuccessK=12,trainSuccessAnim=setInterval(function(){
		train_2d.clearRect(0,0,1024,768);
		var t=trainSuccessAnimArr[trainSuccessIndex];
		train_2d.drawImage(resources['game_train_success_anim'],t[0],t[1],t[2],t[3],(trainSuccessX+=2),450,t[2],t[3]);
		if(++trainSuccessIndex>=15){
			trainSuccessIndex=0;
			if(--trainSuccessK<=0){
				clearInterval(trainSuccessAnim);
				resources['train_music'].pause();
				isTrainError=true;
				if(isTrainError&&isCharaError){
					G.show($success);
					gameOver();
				}
			}
		}
	},1000/60),isTrainError=false;
	// 人物成功动画
	var charaSuccessAnimArr=[
		[0,0,86,82],[86,0,87,82],[173,0,89,81],[262,0,90,81],[352,0,93,78],
		[0,82,95,70],[95,82,95,70],[190,82,84,70],[274,82,87,75],[361,82,97,75],
		[0,157,89,75],[89,157,90,72],[179,157,100,72],[279,157,100,70],[379,157,100,70],
		[0,232,84,71],[84,232,87,71],[171,232,85,74],[256,232,87,74],[343,232,87,84]
	],charaSuccessIndex=0,charaSuccessX=50,charaSuccessK=3,charaSuccessAnim=setInterval(function(){
		chara_2d.clearRect(0,0,1024,768);
		var c=charaSuccessAnimArr[charaSuccessIndex];
		chara_2d.drawImage(resources['game_chara_success_anim'],c[0],c[1],c[2],c[3],(charaSuccessX+=10),550-c[3],c[2],c[3]);
		if(++charaSuccessIndex>=19){
			charaSuccessIndex=0;
			if(--charaSuccessK<=0){
				clearInterval(charaSuccessAnim);
				resources['win2_music'].play();
				resources['win_music'].play();
				// resources['win2_music'].onended=function(){
				// 	resources['win_music'].play();
				// };
				chara_2d.clearRect(0,0,1024,768);
				chara_2d.drawImage(resources['game_chara_success_anim'],343,232,87,84,600,445,87,84);
				isCharaError=true;
				if(isTrainError&&isCharaError){
					G.show($success);
					gameOver();
				}
			}
		}
	},50),isCharaError=false;
}
function toError(){
	resources['train_music'].play();
	// 火车成功动画
	var trainSuccessAnimArr=[
		[0,0,209,64],
		[0,64,209,67],
		[0,131,209,66],
		[0,197,209,66],
		[0,263,209,65],
		[0,328,209,65],
		[0,393,209,65],
		[0,458,209,65],
		[0,523,209,66],
		[0,589,209,67],
		[0,656,209,67],
		[0,723,209,66],
		[0,789,209,66],
		[0,855,209,66],
		[0,921,209,66]
	],trainSuccessIndex=0,trainSuccessX=50,trainSuccessK=10,trainSuccessAnim=setInterval(function(){
		train_2d.clearRect(0,0,1024,768);
		var t=trainSuccessAnimArr[trainSuccessIndex];
		train_2d.drawImage(resources['game_train_success_anim'],t[0],t[1],t[2],t[3],(trainSuccessX+=4),450,t[2],t[3]);
		if(++trainSuccessIndex>=15){
			trainSuccessIndex=0;
			if(--trainSuccessK<=0){
				clearInterval(trainSuccessAnim);
				resources['train_music'].pause();
				// 火车失败动画
				var trainErrorAnimArr=[
					[0,0,210,66],[210,0,210,66],
					[0,66,211,67],[211,66,209,67],
					[0,133,207,77],[207,133,204,77],
					[0,210,206,91],[206,210,206,91],
					[0,301,208,101],[208,301,207,101],
					[0,402,208,100],[208,402,207,100],
					[0,502,208,100],[208,503,207,100],
					[0,602,208,102],[208,602,207,102],
					[0,704,208,102],[208,704,207,102],
					[0,806,208,102],[208,806,207,102],
					[0,906,207,100]
				],trainErrorIndex=0,trainErrorX=trainSuccessX+2,trainErrorK=1,trainErrorAnim=setInterval(function(){
					train_2d.clearRect(0,0,1024,768);
					var t=trainErrorAnimArr[trainErrorIndex];
					train_2d.drawImage(resources['game_train_error_anim'],t[0],t[1],t[2],t[3],trainErrorX,470,t[2],t[3]);
					if(++trainErrorIndex>=21){
						clearInterval(trainErrorAnim);
						console.log('火车失败');
						isTrainError=true;
						if(isTrainError&&isCharaError){
							G.show($error);
							gameOver();
						}
					}
				},1000/60);

			}
		}
	},30),isTrainError=false;
	
	// 人物成功动画
	var charaSuccessAnimArr=[
		[0,0,86,82],[86,0,87,82],[173,0,89,81],[262,0,90,81],[352,0,93,78],
		[0,82,95,70],[95,82,95,70],[190,82,84,70],[274,82,87,75],[361,82,97,75],
		[0,157,89,75],[89,157,90,72],[179,157,100,72],[279,157,100,70],[379,157,100,70],
		[0,232,84,71],[84,232,87,71],[171,232,85,74],[256,232,87,74],[343,232,87,84]
	],charaSuccessIndex=0,charaSuccessX=50,charaSuccessK=2,charaSuccessAnim=setInterval(function(){
		chara_2d.clearRect(0,0,1024,768);
		var c=charaSuccessAnimArr[charaSuccessIndex];
		chara_2d.drawImage(resources['game_chara_success_anim'],c[0],c[1],c[2],c[3],(charaSuccessX+=10),550-c[3],c[2],c[3]);
		if(++charaSuccessIndex>=19){
			charaSuccessIndex=0;
			if(--charaSuccessK<=0){
				clearInterval(charaSuccessAnim);
				// 人物失败动画
				var charaErrorAnimArr=[
					[0,0,86,74],[86,0,87,74],[173,0,88,73],[261,0,92,73],[353,0,91,70],
					[0,74,94,70],[94,74,96,71],[190,74,85,71],[275,74,86,75],[361,74,87,75],
					[0,149,88,75],[88,149,92,72],[180,149,92,72],[272,149,94,70],[366,149,96,70],
					[0,224,84,71],[84,224,87,71],[171,224,85,74],[256,224,86,74],[342,224,76,75],[418,224,93,52],
					[0,300,98,52],[98,300,106,52],[204,300,113,51],[317,300,119,51],
					[0,353,126,51],[126,353,132,53]
				],charaErrorIndex=0,charaErrorX=charaSuccessX+3,charaErrorK=1,charaErrorAnim=setInterval(function(){
					chara_2d.clearRect(0,0,1024,768);
					var c=charaErrorAnimArr[charaErrorIndex];
					if(charaErrorIndex==20){
						resources['fall_music'].play();
						resources['fall_music'].onended=function(){
							resources['dizzy_music'].play();
						};
					}
					chara_2d.drawImage(resources['game_chara_error_anim'],c[0],c[1],c[2],c[3],(charaErrorX+=3),540-c[3],c[2],c[3]);
					if(++charaErrorIndex>=27){
						charaErrorIndex=0;
						if(--charaErrorK<=0){
							clearInterval(charaErrorAnim);
							console.log('人物失败');
							isCharaError=true;
							if(isTrainError&&isCharaError){
								G.show($error);
								gameOver();
							}
						}
					}
				},50);

			}
		}
	},100),isCharaError=false;
}

function gameOver(){
	G.show($confirm_layer);
	confirm_2d.clearRect(0,0,1024,768);
	confirm_2d.save();
	confirm_2d.globalAlpha=0.5;
	confirm_2d.fillRect(0,0,1024,768);
	confirm_2d.restore();
	G.show($replayBtn);
	G.show($returnBtn);
}